package fun.qu_an.velocity.whitelist.config;

import fun.qu_an.lib.basic.config.AnnotationConfig;
import fun.qu_an.lib.basic.config.ConfigKey;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static fun.qu_an.velocity.whitelist.VelocityWhitelist.DATA_DIRECTORY;

public class VelocityWhitelistConfig extends AnnotationConfig {
	public static final VelocityWhitelistConfig CONFIG = new VelocityWhitelistConfig(DATA_DIRECTORY.resolve("config.toml"));
	@Getter
	@ConfigKey(comment = """
		使用玩家名查询玩家UUID的API网址
		默认为Mojang官方：https://api.mojang.com/users/profiles/minecraft/""")
	private @NotNull String uuidApi = "https://api.mojang.com/users/profiles/minecraft/";
	@Getter
	@ConfigKey(comment = """
		使用UUID查询玩家档案的API网址
		默认为Mojang官方：https://sessionserver.mojang.com/session/minecraft/profile/""")
	private @NotNull String profileApi = "https://sessionserver.mojang.com/session/minecraft/profile/";
	@Getter
	@ConfigKey(comment = "是否启用白名单")
	private boolean enableWhitelist = true;
	@ConfigKey(path = "permission.use", comment = """
		拥有使用权的玩家列表
		可以使用“add”、“remove”、“on”、“off”、“reload”指令""")
	private final List<String> use = new ArrayList<>();
	@ConfigKey(path = "permission.admin", comment = """
		拥有管理权的玩家列表（包含使用权）
		可以额外使用“permission add”、“permission remove”指令""")
	private final List<String> admin = new ArrayList<>();

	private VelocityWhitelistConfig(@NotNull Path configPath) {
		super(configPath);
	}

	@Override
	public void load() {
		super.load();
		boolean b = false;
		if (!uuidApi.endsWith("/")) {
			uuidApi = uuidApi + "/";
			b = true;
		}
		if (!profileApi.endsWith("/")) {
			profileApi = profileApi + "/";
			b = true;
		}
		if (b) saveAsync();
	}

	public void enableWhitelist() {
		setEnableWhitelist(true);
	}

	public void disableWhitelist() {
		setEnableWhitelist(false);
	}

	private void setEnableWhitelist(boolean enabled) {
		if (enabled != enableWhitelist) {
			enableWhitelist = enabled;
			saveAsync();
		}
	}

	public void addUsePermission(String player) {
		for (String s : use) {
			if (s.equalsIgnoreCase(player)) return;
		}
		for (String s : admin) {
			if (s.equalsIgnoreCase(player)) return;
		}
		use.add(player);
		saveAsync();
	}

	public boolean hasUsePermission(String player) {
		for (String s : use) {
			if (s.equalsIgnoreCase(player)) return true;
		}
		for (String s : admin) {
			if (s.equalsIgnoreCase(player)) return true;
		}
		return false;
	}

	public void addAdminPermission(String player) {
		for (String s : admin) {
			if (s.equalsIgnoreCase(player)) return;
		}
		admin.add(player);
		saveAsync();
	}

	public boolean hasAdminPermission(String player) {
		for (String s : admin) {
			if (s.equalsIgnoreCase(player)) return true;
		}
		return false;
	}

	public boolean removePermission(String player) {
		boolean b = false;
		for (String s : use) {
			if (s.equalsIgnoreCase(player)) {
				b = true;
				player = s;
				break;
			}
		}
		if (!b) for (String s : admin) {
			if (s.equalsIgnoreCase(player)) {
				b = true;
				player = s;
				break;
			}
		}
		if (b) {
			use.remove(player);
			admin.remove(player);
			saveAsync();
			return true;
		}
		return false;
	}

	public @Unmodifiable Collection<String> usePermissionPlayers() {
		return List.copyOf(use);
	}

	public @Unmodifiable Collection<String> adminPermissionPlayers() {
		return List.copyOf(admin);
	}
}
